package mipo.top.base.controller;

import mipo.top.base.Dto.EmailDto;
import mipo.top.base.Dto.EmailRegisterDto;
import mipo.top.base.Dto.PhoneRegisterDto;
import mipo.top.base.service.IVerifyCodeUtilsService;
import mipo.top.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeUtilsController {

    @Autowired
    public IVerifyCodeUtilsService verifyCodeUtilsService;

    @GetMapping("/image/{imageCodeKey}")
    public JsonResult imgCode(@PathVariable("imageCodeKey") String imageCodeKey){
        try {
            String ImgCode = verifyCodeUtilsService.ImgCode(imageCodeKey);
            return JsonResult.me().setResultData(ImgCode);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }
    @PostMapping("/smsCode")
    public JsonResult SmsCode(@RequestBody @Valid PhoneRegisterDto phoneRegisterDto){
        try {
            verifyCodeUtilsService.SmsCode(phoneRegisterDto);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }
    @PostMapping("/emailCode")
    public JsonResult emailCode(@RequestBody @Valid EmailRegisterDto dto){
        try {
            verifyCodeUtilsService.emailCode(dto);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }

    @GetMapping("/phoneCode/")
    public JsonResult phoneCode(@PathVariable("phone") String phone){
        try{
            verifyCodeUtilsService.phoneCode(phone);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
    }

}
